package com.example.service;

import com.example.common.enums.ModuleEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Messages;
import com.example.entity.Notebook;
import com.example.entity.Works;
import com.example.mapper.MessagesMapper;
import com.example.mapper.NotebookMapper;
import com.example.mapper.WorksMapper;
import com.example.utils.TimeUtils;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MessagesService {
    @Resource
    MessagesMapper messagesMapper;
    @Resource
    NotebookMapper notebookMapper;
    @Resource
    WorksMapper worksMapper;

    public void add(Messages messages,String dynamicName) {
        Account user = TokenUtils.getCurrentUser();
        messages.setNotifierId(user.getId());
        messages.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        messages.setDelTag("0");
        String content;
        if (messages.getModule().equals(ModuleEnum.WORKS.getValue())) {
            content = user.getName() + messages.getOptType() + "了你的" + messages.getModule() + " <a target='_blank' href='/front/workDetail?id=" + messages.getFid() + "'>" + dynamicName + "</a>";
        } else {
            content = user.getName() + messages.getOptType() + "了你的" + messages.getModule() + " <a target='_blank' href='/front/notebookDetail?id=" + messages.getFid() + "'>" + dynamicName + "</a>";
        }
        messages.setContent(content);
        messagesMapper.insert(messages);
    }

    /**
     * 返回审核信息
     */
    public void addMessage(Messages messages){
        messages.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        messages.setDelTag("0");
        String content;
        String dynamicName;
        if(messages.getModule().equals(ModuleEnum.NOTEBOOK.getValue())){
            Notebook notebook=notebookMapper.selectById(messages.getFid());
            dynamicName=notebook.getTitle();
            messages.setUserId(notebook.getUserId());
        }else{
            Works works=worksMapper.selectById(messages.getFid());
            dynamicName=works.getName();
            messages.setUserId(works.getUserId());
        }
        if (messages.getModule().equals(ModuleEnum.WORKS.getValue())) {
            content = "你的" + messages.getModule() + " <a target='_blank' href='/front/workDetail?id=" + messages.getFid() + "'>" + dynamicName + "</a>"+"审核不通过,原因是:"+messages.getReason();
        } else {
            content = "你的" + messages.getModule() + " <a target='_blank' href='/front/notebookDetail?id=" + messages.getFid() + "'>" + dynamicName + "</a>"+"审核不通过,原因是:"+messages.getReason();
        }
        messages.setContent(content);
        messagesMapper.insert(messages);
    }

    public void updateReadStatus() {
        Integer userId = TokenUtils.getCurrentUser().getId();
        messagesMapper.updateReadStatus(userId);
    }

    public List<Messages> selectAll(Messages messages) {
        List<Messages> list = messagesMapper.selectAll(messages);
        list.forEach(entity->entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return list;
    }

    public PageInfo<Messages> selectPage(Messages messages, Integer pageNum, Integer pageSize){
        Account currentUser = TokenUtils.getCurrentUser();
        messages.setUserId(TokenUtils.getCurrentUser().getId());
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            messages.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Messages> list=messagesMapper.selectAll(messages);
        list.forEach(entity->entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return PageInfo.of(list);
    }
    public Messages selectById(Integer id){
        Messages messages=messagesMapper.selectById(id);
        messages.setReleaseTime(TimeUtils.TimestampToDateFormat(messages.getCreateTime()));
        return messages;
    }
    public void deleteById(Integer id) {
        messagesMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            messagesMapper.deleteById(id);
        }
    }
}
